package com.alibaba.citrus.cr.mn.order.center.facade.function.convert;

import com.alibaba.citrus.cr.mn.order.center.common.constants.ForecastConstants;
import com.alibaba.fastjson.JSONObject;
import com.epoch.app.bcorder.platform_enhance.SaleOrderTag;
import com.epoch.app.crforecastsales.domian.forecastorder.model.ForecastOrder;
import com.epoch.app.crforecastsales.sdo.ForecastOrderSDO;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class ForecastOrderToSDOConvertor {

    public static ForecastOrderSDO convert(ForecastOrder forecastOrder){

        ForecastOrderSDO forecastOrderSDO = new ForecastOrderSDO();
        forecastOrderSDO.setId(String.valueOf(forecastOrder.getId()));
        forecastOrderSDO.setOrderBizId(forecastOrder.getOrderBizId());
        forecastOrderSDO.setOuterOrderId(forecastOrder.getOuterOrderId());
        forecastOrderSDO.setSourceOrderNo(forecastOrder.getSourceOrderNo());
        forecastOrderSDO.setBizCode(forecastOrder.getBizCode());
        forecastOrderSDO.setBusinessCode(forecastOrder.getBusinessCode());
        forecastOrderSDO.setOrderTag(forecastOrder.getOrderTag());
        forecastOrderSDO.setBisUnitCode(forecastOrder.getBisUnitCode());
        forecastOrderSDO.setOrganizationCode(forecastOrder.getOrganizationCode());
        forecastOrderSDO.setOrganizationName(forecastOrder.getOrganizationName());
        forecastOrderSDO.setSaleChannelCode(forecastOrder.getSaleChannelCode());
        forecastOrderSDO.setSaleChannelName(forecastOrder.getSaleChannelName());
        forecastOrderSDO.setDepartmentCode(forecastOrder.getDepartmentCode());
        forecastOrderSDO.setDepartmentName(forecastOrder.getDepartmentName());
        forecastOrderSDO.setChannelCode(forecastOrder.getChannelCode());
        forecastOrderSDO.setChannelName(forecastOrder.getChannelName());
        forecastOrderSDO.setShipAddressCode(forecastOrder.getShipAddressCode());
        forecastOrderSDO.setShipAddressInfo(forecastOrder.getShipAddressInfo());
        forecastOrderSDO.setProductGroupCode(forecastOrder.getProductGroupCode());
        forecastOrderSDO.setProductGroupName(forecastOrder.getProductGroupName());
        forecastOrderSDO.setBusinessType(forecastOrder.getBusinessType());
        forecastOrderSDO.setBusinessTypeName(forecastOrder.getBusinessTypeName());
        forecastOrderSDO.setContact(forecastOrder.getContact());
        forecastOrderSDO.setContactPhone(forecastOrder.getContactPhone());
        forecastOrderSDO.setRemark(forecastOrder.getRemark());
        forecastOrderSDO.setAddRemark(forecastOrder.getAddRemark());
        forecastOrderSDO.setWareHouseCode(forecastOrder.getWareHouseCode());
        forecastOrderSDO.setWareHouseName(forecastOrder.getWareHouseName());
        forecastOrderSDO.setShippingWarehouseCode(forecastOrder.getShippingWarehouseCode());
        forecastOrderSDO.setShippingWarehouseName(forecastOrder.getShippingWarehouseName());
        forecastOrderSDO.setOrderTotalFee(forecastOrder.getOrderTotalFee());
        forecastOrderSDO.setTotalActivityFee(forecastOrder.getTotalActivityFee());
        forecastOrderSDO.setTotalReplyAmount(forecastOrder.getTotalReplyAmount());
        forecastOrderSDO.setTotalPredictAmount(forecastOrder.getTotalPredictAmount());
        forecastOrderSDO.setCreditBalance(forecastOrder.getCreditBalance());
        forecastOrderSDO.setStatus(forecastOrder.getStatus());
        forecastOrderSDO.setReplyTime(forecastOrder.getReplyTime());
        forecastOrderSDO.setReplyStatus(forecastOrder.getReplyStatus());
        forecastOrderSDO.setRefuseTime(forecastOrder.getRefuseTime());
        forecastOrderSDO.setRefuseStatus(forecastOrder.getRefuseStatus());
        forecastOrderSDO.setConfirmTime(forecastOrder.getConfirmTime());
        forecastOrderSDO.setConfirmStatus(forecastOrder.getConfirmStatus());
        forecastOrderSDO.setDealStatus(forecastOrder.getDealStatus());
        forecastOrderSDO.setFinishedTime(forecastOrder.getFinishedTime());
        forecastOrderSDO.setIdemId(forecastOrder.getIdemId());
        forecastOrderSDO.setVaildBeginTime(forecastOrder.getVaildBeginTime());
        forecastOrderSDO.setValidEndTime(forecastOrder.getValidEndTime());
        forecastOrderSDO.setAreaCode(forecastOrder.getAreaCode());
        forecastOrderSDO.setSalesmanCode(forecastOrder.getSalesmanId());
        forecastOrderSDO.setSalesmanName(forecastOrder.getSalesmanName());
        forecastOrderSDO.setGmtCreate(forecastOrder.getGmtCreate());
        forecastOrderSDO.setGmtModified(forecastOrder.getGmtModified());
        forecastOrderSDO.setCreatorId(forecastOrder.getCreatorId());
        forecastOrderSDO.setCreatorName(forecastOrder.getCreatorName());
        forecastOrderSDO.setModifierId(forecastOrder.getModifierId());
        forecastOrderSDO.setModifierName(forecastOrder.getModifierName());
        forecastOrderSDO.setSyncVersion(forecastOrder.getSyncVersion());
        forecastOrderSDO.setOrderTypeCode(forecastOrder.getOrderTypeCode());
        forecastOrderSDO.setOrderType(forecastOrder.getOrderType());
        forecastOrderSDO.setOrderTypeName(forecastOrder.getOrderTypeName());
        forecastOrderSDO.setCreateReason(forecastOrder.getCreateReasonCode());
        forecastOrderSDO.setCreateReasonName(forecastOrder.getCreateReasonName());
        forecastOrderSDO.setTransportModeCode(forecastOrder.getTransportModeCode());
        forecastOrderSDO.setTransportModeName(forecastOrder.getTransportModeName());
        forecastOrderSDO.setCustomerReferenceNo(forecastOrder.getCustomerPurchaseOrderNo());
        if (forecastOrder.getCustomerPurchaseDate() != null) {
            forecastOrderSDO.setCustomerPurchaseDate(forecastOrder.getCustomerPurchaseDate());
        }
        forecastOrderSDO.setCustomerExpectTDate(forecastOrder.getCustomerExpectTDate());
//        Map<String, String> enumMap = dictValuesQueryRepository.queryValues("forecastTag");
//        String forecastTag = enumMap.get(forecastOrder.getOrderType());
//        Map<String, String> shipMethod = dictValuesQueryRepository.queryValues("shipMethod");
//        String transportMode = shipMethod.get(forecastOrder.getTransportModeCode());

//        QueryDictionaryRequest queryDictionaryRequest = new QueryDictionaryRequest();
        //获取单据原因的名称
//        if (StringUtils.isNotBlank(forecastOrder.getCreateReason())) {
//            queryDictionaryRequest.setDictCode(ForecastConstants.ORDER_REASON_TYPE);
//            Result<DictionarySDO> dictionarySDOResult = dictionaryQueryService.querySingle(queryDictionaryRequest);
//            dictionarySDOResult.getResult().getDictValueList().stream().forEach(x -> {
//                if (x.getValueData().equals(forecastOrder.getCreateReason())) {
//                    forecastOrderSDO.setCreateReasonName(forecastOrder.getCreateReasonName());
//                }
//            });
//        }
        //获取配送方式名称
//        if (StringUtils.isNotBlank(forecastOrder.getTransportModeCode())) {
//            queryDictionaryRequest.setDictCode(ForecastConstants.SHIP_METHOD_TYPE);
//            Result<DictionarySDO> dictionarySDOResult = dictionaryQueryService.querySingle(queryDictionaryRequest);
//            dictionarySDOResult.getResult().getDictValueList().stream().forEach(x -> {
//                if (x.getValueData().equals(forecastOrder.getTransportModeCode())) {
//                    forecastOrderSDO.setTransportModeName(x.getValueName());
//                }
//            });
//        }

        Map features = StringUtils.isNotBlank(forecastOrder.getFeatures())? JSONObject.parseObject(forecastOrder.getFeatures(),Map.class):new HashMap();
        forecastOrderSDO.setAppCreate(Boolean.FALSE);
        if (MapUtils.isNotEmpty(features)) {
            if (Objects.nonNull(features.get(ForecastConstants.OS))&&StringUtils.isNotBlank(String.valueOf(features.get(ForecastConstants.OS)))){
                String os = String.valueOf(features.get(ForecastConstants.OS));
                if ("app".equalsIgnoreCase(os)){
                    forecastOrderSDO.setAppCreate(Boolean.TRUE);
                }
            }
        }
        forecastOrderSDO.setAddressName(forecastOrder.getShipToPartyName());

        forecastOrderSDO.setFromSys(forecastOrder.getFromSys());
        forecastOrderSDO.setCustomerCode(forecastOrder.getSoldToPartyCode());
        forecastOrderSDO.setCustomerName(forecastOrder.getSoldToPartyName());
        forecastOrderSDO.setReceiveAddressCode(forecastOrder.getShipToPartyCode());
        forecastOrderSDO.setReceiveDetailAddress(forecastOrder.getShipToPartyName());
        forecastOrderSDO.setBusinessUnitCode(forecastOrder.getBusinessUnitCode());
        forecastOrderSDO.setBusinessUnitName(forecastOrder.getBusinessUnitName());
        forecastOrderSDO.setSalesOrganizationCode(forecastOrder.getSalesOrganizationCode());
        forecastOrderSDO.setSalesOrganizationName(forecastOrder.getSalesOrganizationName());
        forecastOrderSDO.setSalesChannelCode(forecastOrder.getSalesChannelCode());
        forecastOrderSDO.setSalesChannelName(forecastOrder.getSalesChannelName());
        forecastOrderSDO.setSalesDepartmentCode(forecastOrder.getSalesDepartmentCode());
        forecastOrderSDO.setSalesDepartmentName(forecastOrder.getSalesDepartmentName());

        //兼容订单平台字段
        if (StringUtils.isBlank(forecastOrderSDO.getCustomerCode()) && StringUtils.isNotBlank(forecastOrder.getCustomerCode())) {
            forecastOrderSDO.setCustomerCode(forecastOrder.getCustomerCode());
        }
        if (StringUtils.isBlank(forecastOrderSDO.getCustomerName()) && StringUtils.isNotBlank(forecastOrder.getCustomerName())) {
            forecastOrderSDO.setCustomerName(forecastOrder.getCustomerName());
        }
        if (StringUtils.isBlank(forecastOrderSDO.getReceiveAddressCode()) && StringUtils.isNotBlank(forecastOrder.getReceiveAddressCode())) {
            forecastOrderSDO.setReceiveAddressCode(forecastOrder.getReceiveAddressCode());
        }
        if (StringUtils.isBlank(forecastOrderSDO.getReceiveDetailAddress()) && StringUtils.isNotBlank(forecastOrder.getReceiveDetailAddress())) {
            forecastOrderSDO.setReceiveDetailAddress(forecastOrder.getReceiveDetailAddress());
        }
        if (StringUtils.isBlank(forecastOrderSDO.getBusinessUnitCode()) && StringUtils.isNotBlank(forecastOrder.getBusinessUnitCode())) {
            forecastOrderSDO.setBusinessUnitCode(forecastOrder.getBisUnitCode());
            forecastOrderSDO.setBusinessUnitName("-----");
        }
        if (StringUtils.isBlank(forecastOrderSDO.getSalesOrganizationCode()) && StringUtils.isNotBlank(forecastOrder.getOrganizationCode())) {
            forecastOrderSDO.setSalesOrganizationCode(forecastOrder.getOrganizationCode());
        }
        if (StringUtils.isBlank(forecastOrderSDO.getSalesOrganizationName()) && StringUtils.isNotBlank(forecastOrder.getOrganizationName())) {
            forecastOrderSDO.setSalesOrganizationName(forecastOrder.getOrganizationName());
        }
        if (StringUtils.isBlank(forecastOrderSDO.getSalesChannelCode()) && StringUtils.isNotBlank(forecastOrder.getSaleChannelCode())) {
            forecastOrderSDO.setSalesChannelCode(forecastOrder.getSaleChannelCode());
        }
        if (StringUtils.isBlank(forecastOrderSDO.getSalesChannelName()) && StringUtils.isNotBlank(forecastOrder.getSaleChannelName())) {
            forecastOrderSDO.setSalesChannelName(forecastOrder.getSaleChannelName());
        }
        if (StringUtils.isBlank(forecastOrderSDO.getSalesDepartmentCode()) && StringUtils.isNotBlank(forecastOrder.getDepartmentCode())) {
            forecastOrderSDO.setSalesDepartmentCode(forecastOrder.getDepartmentCode());
        }
        if (StringUtils.isBlank(forecastOrderSDO.getSalesDepartmentName()) && StringUtils.isNotBlank(forecastOrder.getDepartmentName())) {
            forecastOrderSDO.setSalesDepartmentName(forecastOrder.getDepartmentName());
        }
        if (StringUtils.isBlank(forecastOrderSDO.getShippingWarehouseCode()) && StringUtils.isNotBlank(forecastOrder.getWareHouseCode())) {
            forecastOrderSDO.setShippingWarehouseCode(forecastOrder.getWareHouseCode());
        }
        if (StringUtils.isBlank(forecastOrderSDO.getShippingWarehouseName()) && StringUtils.isNotBlank(forecastOrder.getWareHouseName())) {
            forecastOrderSDO.setShippingWarehouseName(forecastOrder.getWareHouseName());
        }
        if (StringUtils.isBlank(forecastOrderSDO.getCustomerReferenceNo()) && StringUtils.isNotBlank(forecastOrder.getCustomerPurchaseOrderNo())) {
            forecastOrderSDO.setCustomerReferenceNo(forecastOrder.getCustomerPurchaseOrderNo());
        }

        if (StringUtils.isBlank(forecastOrderSDO.getOrderType()) && StringUtils.isNotBlank(forecastOrder.getOrderType())) {
            forecastOrderSDO.setOrderType(forecastOrder.getOrderType());
            forecastOrderSDO.setOrderTypeName(SaleOrderTag.of(forecastOrder.getOrderType()).getDesc());
        }
        if (StringUtils.isBlank(forecastOrderSDO.getCreateReason()) && StringUtils.isNotBlank(forecastOrder.getCreateReason())) {
            forecastOrderSDO.setCreateReason(forecastOrder.getCreateReason());
            forecastOrderSDO.setCreateReasonName("-----");
        }

        return forecastOrderSDO;
    }
}
